<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="栏目ID">
          <a-input placeholder="请输入栏目ID" v-decorator="['channelId', {}]" />

        </a-form-item>-->
        <!--<a-col :span="6">
          <a-form-item label="栏目ID">
            <a-select v-model="wrapperCol" placeholder="请选择性别">
              <a-select-option value="">请选择性别查询</a-select-option>
              <a-select-option value="1">男性</a-select-option>
              <a-select-option value="2">女性</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>-->

        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户ID">
          <a-input placeholder="请输入用户ID" v-decorator="['userId', {}]" />
        </a-form-item>-->
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="录入部门">
          <a-input placeholder="请输入录入部门" v-decorator="['departId', {}]" />
        </a-form-item>-->
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="站点ID">
          <a-input placeholder="请输入站点ID" v-decorator="['siteId', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="问题描述">
          <a-input placeholder="请输入标题" v-decorator="['title', {}]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="短标题">
          <a-input placeholder="请输入短标题" v-decorator="['shortTitle', {}]" />
        </a-form-item>-->
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="标题颜色">
          <a-input placeholder="请输入标题颜色" v-decorator="['titleColor', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发布日期">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'releaseDate', {}]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否加粗">
          <a-input placeholder="请输入是否加粗" v-decorator="['isBold', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否置顶">
          <a-input placeholder="请输入是否置顶" v-decorator="['isTop', {}]" />
        </a-form-item>-->
       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否推荐">
          <a-input placeholder="请输入是否推荐" v-decorator="['isRecommend', {}]" />
        </a-form-item>-->
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态(0:草稿;1:审核中;2:已审核;3:回收站)">
          <a-input placeholder="请输入状态(0:草稿;1:审核中;2:已审核;3:回收站)" v-decorator="['status', {}]" />
        </a-form-item>-->
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型：知识文档、新闻">
          <a-input placeholder="请输入类型：知识文档、新闻" v-decorator="['articleType', {}]" />
        </a-form-item>-->
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否允许评论">
          <a-input-number v-decorator="[ 'commentControl', {}]" />
        </a-form-item>-->
       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="tagStr">
          <a-input placeholder="请输入tagStr" v-decorator="['tagStr', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="原作者">
          <a-input placeholder="请输入原作者" v-decorator="['originAuthor', {}]" />
        </a-form-item>-->
       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="来源">
          <a-input placeholder="请输入来源" v-decorator="['origin', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="来源链接">
          <a-input placeholder="请输入来源链接" v-decorator="['originUrl', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内容">
          <!--<a-input placeholder="请输入描述" v-decorator="['description', {}]" />-->
            <editor-bar :isClear="isClear" @change="change" v-decorator="['description', {}]"></editor-bar>
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否开放：0-开放；1-受限访问（参考cms_article_acl）">
          <a-input-number v-decorator="[ 'isOpen', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="删除标志，0正常，1删除">
          <a-input-number v-decorator="[ 'isDelete', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="附件">
<!--          <a-input placeholder="请输入附件" v-decorator="['attachment', {}]" />-->
          <j-upload v-decorator="['attachment', {}]"></j-upload>
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发布状态，0-未发布；1-已发布">
          <a-input placeholder="请输入发布状态，0-未发布；1-已发布" v-decorator="['state', validatorRules.state ]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="有效日期">
          <a-date-picker v-decorator="[ 'validDate', {}]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="最后修改日期">
          <a-date-picker v-decorator="[ 'lastUptDate', {}]" />
        </a-form-item>-->

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import moment from "moment";
  import { putAction, httpAction } from '@/api/manage'
  import JUpload from '@/components/jeecg/JUpload'
  import ARow from 'ant-design-vue/lib/grid/Row'
  import EditorBar from '../../../components/tools/Editor'
  export default {
    name: "CmsArticleModal",
    components: {
      ARow,
      JUpload,
      EditorBar
    },
    data () {
      return {
        isClear: false,
        detail:"",
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        state:{rules: [{ required: true, message: '请输入发布状态，0-未发布；1-已发布!' }]},
        },
        url: {
          add: "/cxm/cmsArticle/add",
          edit: "/cxm/cmsArticle/edit",
          addByChannel: "/cxm/cmsArticle/addByChannel",
        },
      }
    },
    created () {
    },
    methods: {

      change(val) {
        console.log(val)
        this.model.description = val;
      },
      editbychannelid(cmsChannel){

        console.log(cmsChannel.id);
        let httpurl = this.url.addByChannel;
        let parameter = {
          cmsChannel: cmsChannel
        }
        console.log("par"+parameter);
        putAction(httpurl,parameter).then((res)=>{
          console.log(res)

          this.list = res.result;
          this.form.resetFields();
          this.model = Object.assign({}, res.result);
          this.visible = true;
          this.$nextTick(() => {

            this.form.setFieldsValue(pick(this.model,'channelId','userId','departId','siteId','title','shortTitle','titleColor','isBold','isTop','isRecommend','status','articleType','commentControl','tagStr','originAuthor','origin','originUrl','description','isOpen','isDelete','attachment','state'))
            //时间格式化
            this.form.setFieldsValue({releaseDate:this.model.releaseDate?moment(this.model.releaseDate):null})
            this.form.setFieldsValue({validDate:this.model.validDate?moment(this.model.validDate):null})
            this.form.setFieldsValue({lastUptDate:this.model.lastUptDate?moment(this.model.lastUptDate):null})
          });
        }).finally(() => {

        })


      },




      add (record) {
        this.edit({});
      },
      edit (record) {
        // console.log(channelid);


        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'channelId','userId','departId','siteId','title','shortTitle','titleColor','isBold','isTop','isRecommend','status','articleType','commentControl','tagStr','originAuthor','origin','originUrl','description','isOpen','isDelete','attachment','state'))
		  //时间格式化
          this.form.setFieldsValue({releaseDate:this.model.releaseDate?moment(this.model.releaseDate):null})
          this.form.setFieldsValue({validDate:this.model.validDate?moment(this.model.validDate):null})
          this.form.setFieldsValue({lastUptDate:this.model.lastUptDate?moment(this.model.lastUptDate):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.releaseDate = formData.releaseDate?formData.releaseDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.validDate = formData.validDate?formData.validDate.format():null;
            formData.lastUptDate = formData.lastUptDate?formData.lastUptDate.format():null;

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>